import Mock from 'mockjs'
// get请求从config.url获取参数，post从config.body中获取参数
function param2Obj(url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
}
/* 商品列表 */
let goodsList = []
const count = 100
for (let i = 0; i < count; i++) {
  const a = '#' + Mock.Random.integer(180, 255).toString(16) + Mock.Random.integer(140, 255).toString(16) + Mock.Random.integer(120, 220).toString(16)
  goodsList.push(
    Mock.mock({
      id: Mock.Random.guid(),
      goodPic: Mock.Random.image('100×100', a, ' IMAGE ').replace('×', 'x'),
      goodName: Mock.Random.cparagraph(1), //商品名称
      goodInfo: Mock.Random.cparagraph(3), //商品详情
      price: Mock.Random.integer(100, 2000), //商品价格
      num: Mock.Random.integer(0, 1000), //商品数量
      createTime: Mock.Random.time('yyyy-MM-dd HH:mm:ss'),
    })
  )
}

export default {
  /**
   *获取商品列表
   *需要携带参数 goodName,page,limt(分页大小)
   * @param goodName,page,limit
   * @return {{code:number,count:number,data:*[]}}
   **/
  getGood: (config) => {
    const { goodName, page = 1, pagesize = 20 } = param2Obj(config.url)
    const mockList = goodsList.filter((good) => {
      if (goodName && good.goodName.indexOf(goodName) === -1) return false
      return true
    })
    const pageList = mockList.filter((item, index) => index < pagesize * page && index >= pagesize * (page - 1))
    return {
      code: 200,
      count: mockList.length,
      data: pageList,
      message: '获取商品列表成功',
    }
  },
  /**
   * 增加商品
   * @param name, addr, age, birth, sex
   * @return {{code: number, data: {message: string}}}
   */
  addGood: (config) => {
    const a = '#' + Mock.Random.integer(180, 255).toString(16) + Mock.Random.integer(140, 255).toString(16) + Mock.Random.integer(120, 220).toString(16)
    const { goodName, goodInfo, price, num, createTime } = JSON.parse(config.body)
    goodsList.unshift({
      id: Mock.Random.guid(),
      goodPic: Mock.Random.image('100×100', a, ' IMAGE ').replace('×', 'x'),
      goodName: goodName,
      goodInfo: goodInfo,
      price: price,
      num: num,
      createTime: createTime,
    })
    return {
      code: 201,
      data: {
        message: '添加成功',
      },
    }
  },
  /**
   * 删除用户
   * @param id
   * @return {*}
   */
  deleteGood: (config) => {
    const id = config.body
    if (!id) {
      return {
        code: 400,
        message: '参数不正确',
      }
    } else {
      goodsList = goodsList.filter((u) => u.id !== id)
      return {
        code: 200,
        message: '删除成功',
      }
    }
  },
  /**
   * 修改商品
   * @param id, goodPic, goodName, goodInfo, price, num, createTime
   * @return {{code: number, data: {message: string}}}
   */
  editGood: (config) => {
    const { id, goodPic, goodName, goodInfo, price, num, createTime } = JSON.parse(config.body)
    goodsList.some((u) => {
      if (u.id === id) {
        ;(u.goodPic = goodPic), (u.goodName = goodName), (u.goodInfo = goodInfo), (u.price = price), (u.num = num), (u.createTime = createTime)
        return true
      }
    })
    return {
      code: 201,
      data: {
        message: '修改成功',
      },
    }
  },
}
